/***************************************************************************
 创建者: 华磊
 开始时间: 2019.3.9
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file xx.h
 *  功能: qt modbus server
 *
 *                                                                         *
 ***************************************************************************/
#ifndef MODBUSSERVER_H
#define MODBUSSERVER_H
#include <QModbusServer>
#include <QSerialPortInfo>
#include "modbusdefine.h"

class ModbusServer:public QObject
{
Q_OBJECT
public:
    ModbusServer(ModbusConnection serverTypeIn, QString portNameIn, SerialSettings serialSettingIn
                 , QString ip_port_in, int serverAddressIdIn,int registerSizeIn);
    ~ModbusServer();

public:

    int setServerAddressId(int id);
    int connectPort();
    int disConnectPort();
    int getConnectState();
    int getModbusError();
    int setCoilValue(int id,bool value);
    int setDiscreteInputValue(int id,bool value);
    int setHoldingRegisters(int id, int value);
    int setInputRegister(int id,int value);

    bool getCoilValue(int id,quint16& valueReturn);
    bool getDiscreteInputValue(int id,quint16& valueReturn);
    bool getHoldingRegisters(int id, quint16& valueReturn);
    bool getInputRegister(int id,quint16& valueReturn);


private:
    int initialModbusServer();
    void setupDeviceData();
    int setBit(int id, QModbusDataUnit::RegisterType table, bool value);

private Q_SLOTS:

    void onStateChanged(int state);

    void updateWidgets(QModbusDataUnit::RegisterType table, int address, int size);

    void handleDeviceError(QModbusDevice::Error newError);

private:
    ModbusConnection serverType;
    QModbusServer *modbusDevice;

    QString portName;
    SerialSettings serialSetting;
    QString ipAndPort;
    int serverAddressId;

    RegisterSize registerSize;


};

#endif // MODBUSSERVER_H
